package itsm.isperp.module.controller.performance;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.domain.PageResponse;
import itsm.isperp.framework.utils.HibernateUUIDGenerator;
import itsm.isperp.framework.web.controller.BaseController;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.framework.web.response.ResultMessage;
import itsm.isperp.module.entity.app.AppPosition;
import itsm.isperp.module.entity.performance.PerPerformanceSetting;
import itsm.isperp.module.service.app.AppPositionService;
import itsm.isperp.module.service.performance.PerPerformanceSettingService;
import itsm.isperp.module.service.performance.PerPerformanceSettingUsersService;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 控制器
 * 
 * @author lizx
 * @date 2017-08-26 10:38
 */
@Controller
@RequestMapping("performance/performanceSetting")
public class PerPerformanceSettingController extends
    BaseController<PerPerformanceSetting> {

  @Autowired
  protected PerPerformanceSettingService perPerformanceSettingService;
  
  @Autowired
  protected JdbcTemplate jdbcTemplate;
  
  @Autowired
  protected PerPerformanceSettingUsersService perPerformanceSettingUsersService;

  @Override
  public PerPerformanceSettingService getService() {
    return this.perPerformanceSettingService;
  }


  @Override
  public String getPageTitle() {
    return "绩效考核模板";
  }

  @RequestMapping("edit")
  @Override
  public ModelAndView edit(String id, HttpServletRequest req) throws Exception {
    ModelAndView mv = this.getModelAndView();
    PerPerformanceSetting model = null;
    if (StringUtils.isEmpty(id)) {
      model = new PerPerformanceSetting();
      model.setId(HibernateUUIDGenerator.generate());
    } else {
      model = this.getService().findOne(id);
    }
    AppPositionService appPositionService=ContextHolder.getSpringBean("appPositionService");
    List<AppPosition> listPosition=appPositionService.findAll().getContent();
    mv.addObject("listPosition",listPosition);
    mv.addObject("model",model);
    return mv;
  }
  
  @RequestMapping("saveRelateUser")
  @ResponseBody
  public ResultMessage saveRelateUser(String sourceId, String userName) {
    if(userName.indexOf(",")>-1)
    {
       String[] names=userName.split(",");
       for(String n:names)
       {
         perPerformanceSettingUsersService.saveRelation(sourceId,n);
       }
    }else
    {
      perPerformanceSettingUsersService.saveRelation(sourceId,userName);
    }
    return ResultMessage.newInstance(true);
  }
  
  @RequestMapping("listRelateUser")
  @ResponseBody
  public PageResponse<Map<String,Object>> listRelateUser(String sourceId,JqGridRequest request) {
    request.addFilterRule("setting_id",JqGridFilterRuleOp.eq, sourceId);
      return perPerformanceSettingUsersService.findAllDetailByRequest(request).toPage();
  }
  
  @RequestMapping("listDetail")
  @Override
  @ResponseBody
  public PageResponse<Map<String, Object>> listDetail(JqGridRequest request,
      HttpServletRequest req) {
    String username=ContextHolder.getLoginUsername();
    request.setSearchString(" (creator='"+username+"' OR 'admin'='"+username+"' OR (EXISTS(SELECT 1 FROM per_performance_setting_users WHERE setting_id=t.id AND app_user_name='"+username+"')))");
    return this.getService().findAllDetailByRequest(request).toPage();

  }

}
